<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 头部组件 -->
      <PageTools>
        <template #left>权限设置</template>
        <el-button size="small"
                   type="primay"
                   @click="add(1,'0')">一级权限</el-button>
        <!-- 参数一：type为1一级权限 参数二：父id0 -->
      </PageTools>
      <!-- 卡片 -->
      <el-card style="margin-top: 15px;">
        <el-table border
                  :data="list"
                  row-key="id">
          <el-table-column label="菜单名称"
                           prop="name" />
          <el-table-column label="权限标识"
                           prop="code" />
          <el-table-column label="描述"
                           prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button size="small"
                         type="text"
                         v-if="row.type === 1"
                         @click="add(2,row.id)">添加权限点</el-button>
              <!-- 参数一：二级权限type为2 参数二：row.id为父id为1 -->
              <el-button size="small"
                         type="text"
                         @click="edit(row)">查看</el-button>
              <el-button size="small"
                         type="text"
                         @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 弹出层 -->
        <el-dialog :title="form.id? '编辑权限':'添加权限'"
                   width="40%"
                   :visible.sync="showDialog"
                   @close="close">
          <!-- 表单 -->
          <el-form label-width="100px"
                   :model="form"
                   :rules="rules"
                   ref="form">
            <el-form-item label="权限名称"
                          prop="name">
              <el-input v-model="form.name"
                        placeholder="请输入权限名称" />
            </el-form-item>
            <el-form-item label="权限标识"
                          prop="code">
              <el-input v-model="form.code"
                        placeholder="请输入标识" />
            </el-form-item>
            <el-form-item label="权限描述"
                          prop="description">
              <el-input v-model="form.description"
                        placeholder="请输入权限描述" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch v-model="form.enVisible"
                         active-color="#13ce66"
                         inactive-color="#ff4949"
                         active-value="1"
                         inactive-value="0" />
            </el-form-item>
          </el-form>
          <template #footer>
            <div style="text-align: right;">
              <el-button @click="showDialog = false">取消</el-button>
              <el-button type="primary"
                         @click="addPermission">确定</el-button>
            </div>
          </template>

        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import PageTools from '@/components/PageTools/index.vue'
import { getPermissionList, delPermission, addPermission, editPermission } from '@/api/permisson'
import { listToTree } from '@/utils'
export default {
  name: 'Permission',
  components: {
    PageTools
  },
  data() {
    return {
      list: [],
      showDialog: false, // 弹出层
      form: {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '内容不能为空' }],
        code: [{ required: true, message: '权限标识不能为空' }]
      }
    }
  },
  created() {
    this.getPermissionList() // 获取权限渲染
  },
  methods: {
    async getPermissionList() {
      const data = await getPermissionList()
      // console.log(data)
      this.list = listToTree(data, '0')
    },
    // 删除
    async del(id) {
      try {
        await this.$confirm('你确定要删除吗', '温馨提示', {
          type: 'warning'
        })
      } catch {
        return
      }
      // 发请求
      await delPermission(id)
      this.$message.success('删除成功')
      this.getPermissionList() // 更新
    },
    // 添加权限点
    add(type, pid) {
      this.form.type = type
      this.form.pid = pid
      // console.log(type, pid)
      this.showDialog = true
    },
    // 确定添加
    async addPermission() {
      // 表单校验
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      if (this.form.id) {
        // 更新
        await editPermission(this.form)
        this.$message.success('更新成功')
      } else {
        await addPermission(this.form)
        this.$message.success('添加成功')
      }
      this.getPermissionList() // 获取权限渲染
      this.showDialog = false
    },
    // 重置
    close() {
      // console.log('---')
      this.$refs.form.resetFields()
      this.form = {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 查看
    edit(row) {
      // 显示弹窗
      this.showDialog = true
      // 回显
      console.log(row)
      this.form = { ...row }
    }
  }
}
</script>

<style>
</style>
